package com.nwpu.intimacy.game;

import java.io.ByteArrayOutputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Handler;
import android.util.Base64;

import com.nwpu.intimacy.R;
import com.nwpu.intimacy.friend.FriendInformationActivity;
import com.nwpu.intimacy.game.View.GamePintuLayout;

public class GameActivity extends Activity {

	GamePintuLayout mGameView;

	private static String str1;
	private static String str2;
	private static String str3;
	private static String str4;
	private static String str5;
	private static Bitmap bitmap;

	private JSONObject jsonObject;

	private Handler mHandler;

	private static int i = 0;
	private int j = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);

		mGameView = (GamePintuLayout) findViewById(R.id.id_gameview);

		Bitmap map = bitmap;

		jsonObject = new JSONObject();

		try {

			jsonObject.put("username", str1);
			jsonObject.put("sex", str2);
			jsonObject.put("area", str3);
			jsonObject.put("signature", str4);
			jsonObject.put("add_friend", str5);
			jsonObject.put("portrait", bitmaptoString(bitmap));

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		mGameView.setBitmap(map, 3, jsonObject);

		mHandler = new Handler();
		mHandler.post(new Runnable() {
			@Override
			public void run() {

				if (i == 1) {

					Intent intent = new Intent();
					intent.setClass(getApplicationContext(),
							FriendInformationActivity.class);
					startActivity(intent);

					i = 0;
					j = 1;

					finish();

				}

				if (j == 0) {

					mHandler.postDelayed(this, 1000);

				}
			}
		});

	}

	/**
	 * 静态方法，其他Activity跳转时调用
	 * 
	 * @param str1
	 * @param str2
	 * @param str3
	 * @param str4
	 * @param str5
	 * @param bitmap
	 */
	public static void initInformation(String str1, String str2, String str3,
			String str4, String str5, Bitmap bitmap) {

		GameActivity.str1 = str1;
		GameActivity.str2 = str2;
		GameActivity.str3 = str3;
		GameActivity.str4 = str4;
		GameActivity.str5 = str5;
		GameActivity.bitmap = bitmap;

	}

	/**
	 * 将Bitmap转换成字符串
	 * 
	 * @param bitmap
	 * @return
	 */
	public String bitmaptoString(Bitmap bitmap) {

		// 将Bitmap转换成字符串
		String string = null;
		ByteArrayOutputStream bStream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.PNG, 100, bStream);
		byte[] bytes = bStream.toByteArray();
		string = Base64.encodeToString(bytes, Base64.DEFAULT);
		return string;
	}

	public static void initInformation() {

		i = 1;

	}

}
